import uicurses
import unit
import argparse
import pickle
import configparser
import os.path

class RLRPG:
    def __init__(self,args):
        self.stats = ['hp','mp','str','dex','int']
        self.dmgs = ['physical','magical','fire','cold','lightning','poison','holy','demonic']
        self.skills = ['hp','mp','str','dex','int']

        self.config = configparser.ConfigParser()
        self.config.read(args.config)

        self.classes = configparser.ConfigParser()
        for filename in self.config['data']['classes'].split(','):
            self.classes.read(filename.strip())

        self.items = configparser.ConfigParser()
        for filename in self.config['data']['items'].split(','):
            self.items.read(filename.strip())
        
        self.prefixes = configparser.ConfigParser()
        for filename in self.config['data']['prefixes'].split(','):
            self.prefixes.read(filename.strip())

        self.skills = configparser.ConfigParser()
        for filename in self.config['data']['skills'].split(','):
            self.skills.read(filename.strip())

        self.monsters = configparser.ConfigParser()
        for filename in self.config['data']['monsters'].split(','):
            self.monsters.read(filename.strip())

    def load_player(self):
        if os.path.isfile(args.save):
            save = open(args.save,'rb')
            self.player = pickle.load(save)
        else:
            uname = input('Character name : ')
            uclass = input('Character class : ')
            self.player = unit.Unit(self,uclass.strip(),uname.strip())
        
    def save_player(self):
        save = open(args.save,'wb')
        pickle.dump(self.player,save)
    
    def run_curses(self):
        curses_game = uicurses.UICurses(self)
        curses_game.play()

    def get_class(self,name):
        if name in self.classes:
            return self.classes[name]
        return None

    def get_item(self,name):
        if name in self.items:
            return self.items[name]
        return None
    
    def get_prefix(self,name):
        if name in self.prefixes:
            return self.prefixes[name]
        return None

    def get_skill(self,name):
        if name in self.skills:
            return self.skills[name]
        return None

    def get_monster(self,name):
        if name in self.monsters:
            return self.monsters[name]
        return None

if __name__=="__main__":
    # read arguments
    parser = argparse.ArgumentParser(description='Roguelike-RPG game')
    parser.add_argument('-s','--save',help='save file',default='data/save')
    parser.add_argument('-c','--config',help='configuration file',default='data/rlrpg.conf')
    args = parser.parse_args()

    # run game
    game = RLRPG(args)
    game.load_player()
    game.run_curses()
    # game.save_player()
